package com.departmentstore.controller;

import com.departmentstore.dto.DepartmentProductDTO;
import com.departmentstore.entity.DepartmentProduct;
import com.departmentstore.service.DepartmentProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/department-products")
@Tag(name = "部门商品管理", description = "部门商品的增删改查接口")
public class DepartmentProductController {

    private final DepartmentProductService departmentProductService;

    @Autowired
    public DepartmentProductController(DepartmentProductService departmentProductService) {
        this.departmentProductService = departmentProductService;
    }

    @PostMapping
    @Operation(summary = "创建部门商品", description = "创建一个新的部门商品关联")
    public ResponseEntity<DepartmentProduct> createDepartmentProduct(@Valid @RequestBody DepartmentProduct departmentProduct) {
        return ResponseEntity.ok(departmentProductService.createDepartmentProduct(departmentProduct));
    }

    @PutMapping("/{departmentId}/{productId}")
    @Operation(summary = "更新部门商品", description = "根据部门ID和商品ID更新部门商品信息")
    public ResponseEntity<DepartmentProduct> updateDepartmentProduct(
            @Parameter(description = "部门ID") @PathVariable String departmentId,
            @Parameter(description = "商品ID") @PathVariable String productId,
            @Valid @RequestBody DepartmentProduct departmentProduct) {
        departmentProduct.setDepartmentId(departmentId);
        departmentProduct.setProductId(productId);
        return ResponseEntity.ok(departmentProductService.updateDepartmentProduct(departmentProduct));
    }

    @DeleteMapping("/{departmentId}/{productId}")
    @Operation(summary = "删除部门商品", description = "根据部门ID和商品ID删除部门商品关联")
    public ResponseEntity<Void> deleteDepartmentProduct(
            @Parameter(description = "部门ID") @PathVariable String departmentId,
            @Parameter(description = "商品ID") @PathVariable String productId) {
        departmentProductService.deleteDepartmentProduct(departmentId + "_" + productId);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/{departmentId}/{productId}")
    @Operation(summary = "获取部门商品", description = "根据部门ID和商品ID获取部门商品信息")
    public ResponseEntity<DepartmentProduct> getDepartmentProduct(
            @Parameter(description = "部门ID") @PathVariable String departmentId,
            @Parameter(description = "商品ID") @PathVariable String productId) {
        return ResponseEntity.ok(departmentProductService.getDepartmentProductById(departmentId + "_" + productId));
    }

    @GetMapping
    @Operation(summary = "获取所有部门商品", description = "获取所有部门商品的列表")
    public ResponseEntity<List<DepartmentProductDTO>> getAllDepartmentProducts() {
        return ResponseEntity.ok(departmentProductService.getAllDepartmentProducts());
    }

    @GetMapping("/department/{departmentId}")
    @Operation(summary = "根据部门ID查找商品", description = "根据部门ID查找该部门的所有商品")
    public ResponseEntity<List<DepartmentProduct>> getDepartmentProductsByDepartmentId(
            @Parameter(description = "部门ID") @PathVariable String departmentId) {
        return ResponseEntity.ok(departmentProductService.getDepartmentProductsByDepartmentId(departmentId));
    }

    @GetMapping("/product/{productId}")
    @Operation(summary = "根据商品ID查找部门", description = "根据商品ID查找销售该商品的所有部门")
    public ResponseEntity<List<DepartmentProduct>> getDepartmentProductsByProductId(
            @Parameter(description = "商品ID") @PathVariable String productId) {
        return ResponseEntity.ok(departmentProductService.getDepartmentProductsByProductId(productId));
    }
} 